using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ConnectionEngine.Common;
using dao = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using business = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.Reflection;

namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider
{
    public class SystemAdapter : Microsoft.ConnectionEngine.Adapters.SystemAdapterBase
    {

         /// <summary>
        /// Gets a list of Service Providers this Adapter is able to contact
        /// </summary>
        /// <returns>An object representation of the Service Providers List</returns>
        protected override dao.ServiceProviderList GetAllowedProviderList()
        {
            return business.ServiceProviderManager.GetAllowedServiceProviders(SystemProvider.ServiceProviderID);
        }
        
        /// <summary>
        /// Gets a set of commonly used data for all Adapters.
        /// </summary>
        /// <returns>An object repesentation of commonly used reference data.</returns>
        protected override dao.ReferenceData GetReferenceData()
        {
            return business.RefDataManager.GetRefData();
        }

        protected override System.Xml.XmlDocument AssembleMessage(object objMessage, int msgType)
        {
            /// I will assume that being the Adapter for the Service Provider Register itself, all the messges will be pre-assembled.
            /// So I will just make sure it is a string, load into XML and return the XML document.
            try
            {
                string payloadContent = (string)objMessage;
                System.Xml.XmlDocument payloadXml = new System.Xml.XmlDocument();
                payloadXml.LoadXml(payloadContent);
                return payloadXml;
            }
            catch (Exception ex)
            {
                SystemAdapterException sysEx = new SystemAdapterException(
                    SystemAdapterExceptionCause.Hl7PlayloadCreation,
                    "An error occurred assembling the Xml Message.",
                    ex);
                throw sysEx;
            }
        }


        /// <summary>
        /// Validates a Connection Engine Message
        /// </summary>
        /// <param name="msg">The message to be validated</param>
        /// <returns>
        /// Returns the original message with validation parameters and Receiver URI updated.
        /// In case of error, returns the original message with a error status associated.
        /// </returns>
        public virtual ConnectionEngineMessage ValidateConnectionEngineMessage(ConnectionEngineMessage msg)
        {
            ConnectionEngineMessage result = new ConnectionEngineMessage();
            try
            {

                result = business.MessageManager.ValidateConnectionEngineMessage(msg);
            }
            catch (SystemAdapterException sysEx)
            {
                ExceptionPolicy.HandleException(sysEx, "HCE Policy");

                result.Header.Status.Code = (int)sysEx.Cause;
                result.Header.Status.Description = SystemAdapterException.GetExceptionDescription(sysEx.Cause);

            }
            catch (Exception ex)
            {
                {
                    ExceptionPolicy.HandleException(ex, "HCE Policy");
                    throw;
                }
            }
            return result;
        }

    }
}
